--
-- Author: yawei.zhu
-- Date: 2018-09-26 15:13:43
--
-- BossModel
--
local WorldBossInfo = import(".WorldBossInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

local _TEST = false

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
end

------------------------ receive ------------------------
function ClassRef:onWorldBossNotify(msg)
	local weekDay = app:getWeekDay()
	local bossConfig = GD:getWorldBossConfigByDay(weekDay) 
	if UD:getLevel() < tonumber(bossConfig.level) then -- 未开启世界boss
		return 
	end
    --dump(msg, "WorldBossNotify")
	if msg then --世界boss开启或者有上一轮奖励未领取
		self._worldBossStatusData = {}
		self._worldBossStatusData.open = msg.isOpen or false
		self._worldBossStatusData.bossId = msg.bossID
        self._worldBossStatusData.rewardNotGet = msg.RewardNotGet or false
		self._worldBossStatusData.endTime = GD:getWolrdBossEndTime()

        if msg.isOpen then --只有活动开启的时候才在地图上生成NPC
		    NpcFactory:createActivityNpcById(1, self._worldBossStatusData.bossId)
        else
            -- 移除世界boss Npc
		    NpcFactory:deleteActivityNpc(1)
        end
	else
		self._worldBossStatusData = nil
		-- 移除世界boss Npc
		NpcFactory:deleteActivityNpc(1)
	end

	 --dump(self._worldBossStatusData)
	
	-- 通知到 tasktrackview 里，是否显示世界boss对应的item
	EM:notify("onTaskTrackChange")
end

function ClassRef:getWorldBossStatusData()
	return self._worldBossStatusData
end

-- 服务器数据中世界boss是否开启
function ClassRef:getWorldBossIsOpen()
	return self._worldBossStatusData and self._worldBossStatusData.open or false
end

-- 客户端计算世界boss是否已经结束
function ClassRef:worldBossIsEnded()
	local endTime = GD:getWolrdBossEndTime()
	local serverTime = app.session:getServerTime()
	local remainedTime = math.floor(endTime - serverTime)
	if remainedTime <= 0 then
		return true
	else
		if not self._worldBossStatusData or (self._worldBossStatusData and not self._worldBossStatusData.open) then
			return true
		end
	end
	return false
end

-- 通知有新的女神祝福
function ClassRef:onNotifyGoddessInfo(msg)
	if _TEST then
		msg = {}
		local info = {}
		info.id = 1
		info.residualTimes = 2
		info.cards = {}

		for i = 1, 4 do
			local value = {}
			value.id = i
			value.open = false
			value.quality = i
			info.cards[i] = value
		end
		msg.info = info
		self._testGoddessInfo = info
	end

	-- dump(msg.info)

	app:sendMsg("GoddessWishController", "openView", msg.info)
end

-- 通知女神祝福 神秘奖励
function ClassRef:onNotifyMysteryReward(msg)
	local mysteryCardId = msg.cardId
	app:sendMsg("GoddessWishController", "onNotifyMysteryReward", mysteryCardId)
end

------------------------ request ------------------------
-- 请求野外boss信息
function ClassRef:reqSummonBoss(uid, id)
	local bagModel = app:getInst("NewBagModel")
	-- self:onOperate( "useItem", item.uid, item.tid, 1)
	bagModel:reqUseItem(uid, id, 1)
end

-- 登录时请求当前世界boss状态
function ClassRef:reqWorldBossStatus()
	print("reqWorldBossStatus")
	app.session:request("pb_worldboss.C2S_WorldBossNotify", {})
end

-- 请求开始世界boss战斗
function ClassRef:reqWorldBossBattle()
	print("reqWorldBossBattle")

	app.session:request("pb_worldboss.C2S_WorldBossBattle", {})
end

-- 请求世界boss信息
function ClassRef:reqWorldBossInfo()
	print("reqWorldBossInfo")

	if _TEST then
		self._worldBossInfo = WorldBossInfo.new()
		self._worldBossInfo:test()
		executeFunc(cb, self._worldBossInfo)
		return
	end

    self._isOpenView = true
	app.session:request("pb_worldboss.C2S_WorldBossInfo", {})
end

function ClassRef:onWorldBossInfo(msg)
    if msg then
        self._worldBossInfo = WorldBossInfo.new(msg)
        if self._isOpenView then --打开界面
            app:sendMsg("WorldBossController", "showView", self._worldBossInfo)
            self._isOpenView = false
        else --刷新宝箱数据
            app:sendMsg("WorldBossController", "onUpdateWorldBossInfo", self._worldBossInfo)
        end
    end
end

-- 请求拉取世界boss 小队排名
function ClassRef:reqWorldBossTeamRank()
	print("reqWorldBossTeamRank")

	if _TEST then
		local teams = {}
		for i = 1, 5 do
			local value = {}
			value.rank = i
			value.teamCombat = i * 10000
			value.battleTime = i * 100
			teams[i] = value
		end
		app:sendMsg("BossRankController", "onBossTeamRankUpdate", teams)
		return
	end

	app.session:request("pb_worldboss.C2S_WorldBossSpeedRankInfo", {}, function(msg)
		print("S2C_WorldBossRankSpeedInfo")

		app:sendMsg("BossRankController", "onBossTeamRankUpdate", msg.teams)
	end)
end

-- 请求拉取世界boss 公会排名
function ClassRef:reqWorldBossGuildRank()
	print("reqWorldBossGuildRank")

	if _TEST then
		local guilds = {}
		for i = 1, 5 do
			local value = {}
			value.rank = i
			value.guildName = "guildName_"..i
			value.guildID = i
			value.killedCount = i * 10
			guilds[i] = value
		end
		app:sendMsg("BossRankController", "onBossGuildRankUpdate", guilds)
		return
	end

	app.session:request("pb_worldboss.C2S_WorldBossTotalRankInfo", {}, function(msg)
		print("S2C_WorldBossRankTotalInfo")

		app:sendMsg("BossRankController", "onBossGuildRankUpdate", msg.guilds)
	end)

end

-- 请求选择女神祝福 卡片
-- index: 0-3
function ClassRef:reqGoddessOpenCard(id, index)
	print("C2S_OpenCard index:", index)

	-- dump(self._testGoddessInfo, "preOpen")

	if _TEST then

		local info = {}
		info.id = self._testGoddessInfo.id
		info.residualTimes = self._testGoddessInfo.residualTimes
		info.cards = {}

		for i = 1, 4 do
			local oldData = self._testGoddessInfo.cards[i]
			local value = {}
			value.id = oldData.id
			value.open = oldData.open
			value.quality = oldData.quality
			info.cards[i] = value
		end

		info.cards[index + 1].open = true

		-- 摧毁卡片
		-- local count = 0
		-- for i = 1, 4 do
		-- 	if count >= 2 then break end
		-- 	local cardInfo = info.cards[i]
		-- 	if not cardInfo.open then
		-- 		cardInfo.open = true
		-- 		count = count + 1
		-- 	end
		-- end
		self._testGoddessInfo = info

		app:sendMsg("GoddessWishController", "onOpenCardSuccess", index, self._testGoddessInfo)
		return
	end

	app.session:request("pb_goddessbless.C2S_OpenCard", {id = id, index = index}, function(msg)
		print("S2C_OpenCard")

		if msg and msg.ok then
			-- dump(msg.info)
			app:sendMsg("GoddessWishController", "onOpenCardSuccess", msg.index, msg.info)
		else
			app:sendMsg("GoddessWishController", "revertGoddessStatus") -- revert抽卡状态
		end
	end)
end

-- 开启宝箱
-- 拉取该ID对应的次数
function ClassRef:reqItemUseTimes(itemId, cb)
	print("reqItemUseTimes", itemId)
	local ids = {itemId}
	app.session:request("action.C2S_QueryItemUseTimes", {itemIds = ids}, function(msg)
		-- dump(msg, "S2C_QueryItemUseTimes")
		if msg then 
			for _, data in ipairs(msg.infos) do
				if data.itemId == itemId then
					executeFunc(cb, data)
					break
				end
			end
		end
	end)
end 

-- 世界BOSS领取 奖励宝箱
function ClassRef:reqOpenWorldBossBox(index)
    print("C2S_OpenWorldBossBox")
    app.session:request("pb_worldboss.C2S_OpenWorldBossBox", {typ = index}, function(ret)
        display.pushToast(L("lua_code_text_532"))
    end)
end

return ClassRef
